<?php include "../scripts/editor.php" ?>
<script type="text/javascript">
	cotCanHien[0] = 2;
	cotCanHien[1] = 4;
	var imge = new Image();
	var imgc = new Image();
	imge.src = "../adm/images/expand.gif";
	imgc.src = "../adm/images/collapse.gif";
	function toggleTab(idtab, bntt)
	{
		if(document.getElementById(idtab).style.display == 'block')
		{
			document.getElementById(idtab).style.display = 'none'
			document.getElementById(bntt).src = imge.src
		}
		else
		{
			document.getElementById(idtab).style.display = 'block'
			document.getElementById(bntt).src = imgc.src
		}
	}
	function chon()
    {
		 var masterChecked = document.formgui.master.checked;
        for(var i=1; i<=document.formgui.elements.length;i++)
        {
                if(document.formgui.elements[i].type=="checkbox")
                        document.formgui.elements[i].checked = masterChecked;
        }		       
    }
	function submitform() 
	{  
		 document.formgui.submit(); 
	}
	function ajaxFunction(id){
	    var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            document.getElementById('MaChuyenMuc').innerHTML = xmlHttp.responseText;
	        }
	    }
	    xmlHttp.open('GET', '../adm/baiviet/comboboxchude.php?dmid='+id, true);
	    xmlHttp.send(null);
	}
	function ajaxFunction2(id){
	    var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            document.getElementById('MaChuyenMuc2').innerHTML = xmlHttp.responseText;
	        }
	    }
	    xmlHttp.open('GET', '../adm/baiviet/comboboxchude2.php?dmid='+id, true);
	    xmlHttp.send(null);
	}
</script>
<?php
	$baiviet = new baivietbs();
	$danhsach = $baiviet->LayDanhSach();
	session_start();
	$nhatky = new nhatkybs();
	$nhatky->TenSuKien = "Xem danh sách bài viết";
	$nhatky->MaNguoiDung = $_SESSION["MaND"];
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	$nhatky->ThoiGian = date("Y-m-d H:m:s", time());
	$nhatky->Them();
?>
<form name="formgui"  method="POST" action="index.php?do=xoachonbaiviet">
<table id="rounded-corner" class="display" summary="" border="1">
	<thead>
		<tr>
			<th align="center" colspan="13">
	               	<h2><strong>DANH SÁCH BÀI VIẾT</strong></h2>
			</th>
		</tr>
		<tr>
			<?php echo "<th scope='col' class='rounded-company'><input type='checkbox' name='master' onclick='chon()'/></th>"; ?>
			<th scope="col" class="rounded">STT</th>
			<th scope="col" class="rounded">Chủ đề</th>
			<th scope="col" class="rounded">Tiêu đề</th>
			<th scope="col" class="rounded">Ngày đăng</th>
			<th scope="col" class="rounded">Lượt xem</th>
			<th scope="col" class="rounded">Sửa</th>
			<th scope="col" class="rounded">Xóa</th>
			<th scope="col" class="rounded">Xuất bản</th>
		</tr>
	</thead>
	<tfoot>
            <tr>
            	<th>Chọn</th>
            	<th>STT</th>
                <th></th>
                <th>Tiêu đề</th>
                <th></th>
                <th>Lượt xem</th>
                <th>Sửa</th>
                <th>Xóa</th>   
                <th>Xuất bản</th>          
            </tr>
    </tfoot>
	<tbody>
		<?php
				$stt = 1;
				for($i = 0; $i < ($danhsach == false ? 0 : count($danhsach)); $i++)
				{
					$XuatBan = $danhsach[$i]["XuatBan"];
					$Active = "<img src='images/active.png' alt='Xuất bản' title='' border='0' />";
					$Disable = "<img src='images/khoa.png' alt='Xuất bản' title='' border='0' />";
					echo "<tr>";
					echo "<td><input type='checkbox' name='{$danhsach[$i]["MaBaiViet"]}' /></td>";
					echo "<td>" . ($i + 1) . "</td>";
					echo "<td>{$danhsach[$i]['TenLoaiChuyenMuc']}</td>";
					echo "<td><a href='../news.php?do=baivietchitiet&mbv={$danhsach[$i]["MaBaiViet"]}'>{$danhsach[$i]['TieuDe']}</a></td>";
					echo "<td>{$danhsach[$i]['NgayDang']}</td>";
					echo "<td>{$danhsach[$i]['LuotXem']}</td>";
					echo "<td><a onclick='toggleSua();' href='index.php?do=baiviet&id={$danhsach[$i]["MaBaiViet"]}'><img src='images/user_edit.png' alt='Sửa' title='' border='0' /></a></td>";
					echo "<td><a href='index.php?do=xoabaiviet&id={$danhsach[$i]["MaBaiViet"]}' class='ask'><img src='images/trash.png' alt='Xóa' title='' border='0' /></a></td>";
					echo "<td><a href='index.php?do=thaydoixuatbanbaiviet&id={$danhsach[$i]["MaBaiViet"]}'>"; 
					if ($XuatBan ==1) 
						echo $Active; 
					else 
						echo $Disable;
					echo "</a></td>";
					echo "</tr>";
					$stt++;
				}	
			?>
	</tbody>
</table>

<br/>
<br/>
<p align="center">
	<a href="javascript: submitform()" class="button small blue"><strong>Xóa chọn</strong></a> 
	&nbsp &nbsp &nbsp
	<a href="index.php?do=xoatatcabaiviet"  class="button small blue"><strong>Xóa tất cả</strong></a> 
</p>
<br/>
<br/>
</form>
 <h2>
 	<a style="text-decoration:none"  class="toggle" href="javascript:void(0);" onclick="toggleTab('tgLogin', 'btnLogin')">
		<img id="btnLogin" src="../adm/images/expand.gif" height="11" width="11">
	</a>
 	Thêm mới bài viết
 </h2>   
         <div class="form" id="tgLogin" style="display:none;">
         <form enctype="multipart/form-data" action="index.php?do=thembaiviet_xuly" method="post" onsubmit="return checkPostArtForm(this);">
           	<table style="width:850px">
           		<tr>
	           		<td style="width:10%;height:20px;padding-top:10px;">
	           			<label for="chuyenmuc">Chuyên mục:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<select class='chzn-select' name="MaChuyenMuc" id="" onchange="ajaxFunction(this.value);">
	            			<option value="0">-- Chọn chuyên mục --</option>	                      	
							<?php
								$chuyenmuc = new chuyenmucbs();
								$danhsachchuyenmuc = $chuyenmuc->LayDanhSach();
								
								for($j = 0; $j < ($danhsachchuyenmuc != false ? count($danhsachchuyenmuc) : 0); $j++)
								{							
									echo "<option value='{$danhsachchuyenmuc[$j]["MaChuyenMuc"]}'>{$danhsachchuyenmuc[$j]['TenChuyenMuc']}</option>";
								}
							?>
						</select>	
						<input onclick="location.href = 'index.php?do=chuyenmuc';" class="buton" type="button" name="button" id="btndschuyenmuc" style="margin-left: 5px; cursor: pointer; background: url('images/list_add.png') no-repeat top left;" />        				
	            	</td>
	            </tr>
	            <tr>
	           		<td style="padding-top:10px;">
	           			<label for="loaichuyenmuc">Chủ đề:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	           			<span id="MaChuyenMuc">
		            		<select name="MaLoaiChuyenMuc" id="" onchange="this.setCustomValidity('')">	
		            			<option value="0">-- Chọn chủ đề --</option>
		            		</select>
						</span>
	            	</td>
	            </tr>
	            <tr>
	           		<td style="padding-top:10px;">
	           			<label for="tieude">Tiêu đề:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<input placeholder ="Nhập tiêu đề" tabindex="1" type="text" name="TieuDe" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập tiêu đề')" onchange="this.setCustomValidity('')"/>    
	            	</td>
	            </tr>
	           	<tr>
	           		<td style="padding-top:10px;">
	           			<label for="tomtat">Tóm tắt:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<textarea id="TomTat" name="TomTat" rows="20" cols="36"></textarea>
						<?php KhoiTaoEditor("TomTat", 100); ?> 
	            	</td>
	            </tr>
	            <tr>
	           		<td style="padding-top:10px;">
	           			<label for="hinhanh">Hình ảnh:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<input placeholder ="Nhập hình ảnh" tabindex="1" type="file" name="HinhAnh" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập hình ảnh')" onchange="this.setCustomValidity('')"/>    
	            	</td>
	            </tr>
	            <tr>
	           		<td style="padding-top:10px;">
	           			<label for="tenhinh">Tên hình:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<input placeholder ="Nhập tên hình" tabindex="1" type="text" name="TenHinh" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa nhập tên hình')" onchange="this.setCustomValidity('')"/>    
	            	</td>
	            </tr>
	            <tr>
	            	<td style="padding-top:10px;">
            			<label for="noidung">Nội dung:</label>&nbsp<span class="requirefield">*</span> 
            		</td>
            		<td style="padding-top:10px;">
            			<textarea id="NoiDung" name="NoiDung" rows="20" cols="36"></textarea>
						<?php KhoiTaoEditor("NoiDung", 300); ?>
					</td>
				</tr>
				<p class="requirefields">(*): Các trường bắt buộc phải có.</p>      				
            </table>
            <table style="margin-left: 320px;">
            	<tr>
					<td style="padding-top:15px" colspan="2" align="center">
				        <input class="button small blue" type="submit" name="submit" id="submit" value="Thêm mới" />
				       	&nbsp &nbsp &nbsp
				       	<input class="button small blue" type="reset" name="reset" id="reset" value="Nhập lại" />
				    </td>
            	</tr>
            </table>
         </form>
         </div>         
<br/>

<?php
	// Lấy sách cần sửa và hiển thị vào FORM
	$baiviet = new baivietbs();
	$baiviet->MaBaiViet = $_GET['id'];
	$baivietcansua = $baiviet->LayDanhSach();
	$MaLCM = $baivietcansua[0]['MaLoaiChuyenMuc'];
	$lcm = new loaichuyenmucbs();
	$lcm->MaLoaiChuyenMuc = $MaLCM;
	$dslcm = $lcm->LayDanhSach();
	$MaCM = $dslcm[0]['MaChuyenMuc'];
	session_start();
	$nhatky = new nhatkybs();
	$nhatky->TenSuKien = "Xem thông tin bài viết";
	$nhatky->MaNguoiDung = $_SESSION["MaND"];
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	$nhatky->ThoiGian = date("Y-m-d H:m:s", time());
	$nhatky->Them();
?>

         <div class="form" id="tdSua" style="<?php if($_GET['id']>0) echo "display:block"; else echo "display:none"; ?>">
         <form enctype="multipart/form-data" action="index.php?do=suabaiviet_xuly" method="post" onsubmit="return checkPostArtForm(this);" >
         <h2>Sửa bài viết</h2>
                <table style="width:850px">
                <tr>
	           		<td style="width:10%;height:20px;padding-top:10px;">
	           			<label for="chuyenmuc">Chuyên mục:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<select name="MaChuyenMuc2" id="" onchange="ajaxFunction2(this.value);">
	            		<option value="0">-- Chọn chuyên mục --</option>	                      	
							<?php
								$chuyenmuc = new chuyenmucbs();
								$danhsachchuyenmuc = $chuyenmuc->LayDanhSach();
								
								for($j = 0; $j < ($danhsachchuyenmuc != false ? count($danhsachchuyenmuc) : 0); $j++)
								{	
									if($danhsachchuyenmuc[$j]["MaChuyenMuc"] == $MaCM)					
										echo "<option selected value='{$danhsachchuyenmuc[$j]["MaChuyenMuc"]}'>{$danhsachchuyenmuc[$j]['TenChuyenMuc']}</option>";
									else
										echo "<option value='{$danhsachchuyenmuc[$j]["MaChuyenMuc"]}'>{$danhsachchuyenmuc[$j]['TenChuyenMuc']}</option>";
								}
							?>
						</select>
						<input onclick="location.href = 'index.php?do=chuyenmuc';" class="buton" type="button" name="button" id="btndschuyenmuc" style="margin-left: 5px; cursor: pointer; background: url('images/list_add.png') no-repeat top left;" />        				
	            	</td>
	            </tr>
	            <tr>
	           		<td style="padding-top:10px;">
	           			<label for="loaichuyenmuc">Chủ đề:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<span id="MaChuyenMuc2">
		            		<select name="MaLoaiChuyenMuc2" id="" onchange="this.setCustomValidity('')">	
		            			<option value="0">-- Chọn chủ đề --</option>
		            			<?php
								$LCM = new loaichuyenmucbs();
								$LCM->MaChuyenMuc = $MaCM;
								$DSLCM = $LCM->LayDanhSach();
								
								for($j = 0; $j < ($DSLCM != false ? count($DSLCM) : 0); $j++)
								{
									if($DSLCM[$j]["MaLoaiChuyenMuc"] == $MaLCM)							
										echo "<option selected value='{$DSLCM[$j]["MaLoaiChuyenMuc"]}'>{$DSLCM[$j]['TenLoaiChuyenMuc']}</option>";
									else 
										echo "<option value='{$DSLCM[$j]["MaLoaiChuyenMuc"]}'>{$DSLCM[$j]['TenLoaiChuyenMuc']}</option>";
								}
								?>
							</select>	
						</span>
	            	</td>
	            </tr>
	           	<tr>
	           		<input type="hidden" value="<?php echo $baivietcansua[0]['MaBaiViet']; ?>" readonly="readonly" name="MaBaiViet2" />
                    <input type="hidden" value="<?php echo $baivietcansua[0]['TieuDe']; ?>" readonly="readonly" name="TieuDeCu" />
	           		<td style="padding-top:10px;">
	           			<label for="tieude">Tiêu đề:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<input placeholder ="Nhập tiêu đề" tabindex="1" type="text" name="TieuDe" class="txt" size="54" value="<?php echo $baivietcansua[0]['TieuDe']; ?>" required oninvalid="this.setCustomValidity('Bạn chưa nhập tiêu đề')" onchange="this.setCustomValidity('')"/>
	            	</td>
	            </tr>
	            
	            <tr>
	            	<td style="padding-top:10px;">
            			<label for="tomtat">Tóm tắt:</label>&nbsp<span class="requirefield">*</span> 
            		</td>
            		<td style="padding-top:10px;">
            			<textarea id="TomTat2" name="TomTat2" rows="20" cols="36"><?php echo $baivietcansua[0]['TomTat']; ?></textarea>		
							<script type="text/javascript">
								var editor = CKEDITOR.replace( 'TomTat2',
								{
									// Load the German interface.
									<?php $address = "/WebQTD/scripts/ckfinder/";?>
									language: 'vi',
									// Xem
									filebrowserBrowseUrl : 		'<?php echo $address; ?>ckfinder.html',
									filebrowserImageBrowseUrl : '<?php echo $address; ?>ckfinder.html?type=Images',
									filebrowserFlashBrowseUrl : '<?php echo $address; ?>ckfinder.html?type=Flash',
									// Upload
									filebrowserUploadUrl : 		'<?php echo $address; ?>core/connector/php/connector.php?command=QuickUpload&type=Files',
									filebrowserImageUploadUrl : '<?php echo $address; ?>core/connector/php/connector.php?command=QuickUpload&type=Images',
									filebrowserFlashUploadUrl : '<?php echo $address; ?>core/connector/php/connector.php?command=QuickUpload&type=Flash',
									
									height: 200
								});
								
								CKFinder.setupCKEditor( editor, '<?php echo $address; ?>' ) ;
							</script>
					</td>
				</tr>
				<tr>
                    <input type="hidden" value="<?php echo $baivietcansua[0]['HinhAnh']; ?>" readonly="readonly" name="HinhAnhCu" />
	           		<td style="padding-top:10px;">
	           			<label for="hinhanh">Hình ảnh:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<input placeholder ="Nhập hình ảnh" tabindex="1" type="file" name="HinhAnh2" class="txt" size="54" value="<?php echo $baivietcansua[0]['HinhAnh']; ?>"/>
	            	</td>
	            </tr>
	            <tr>
                    <input type="hidden" value="<?php echo $baivietcansua[0]['TenHinh']; ?>" readonly="readonly" name="TenHinhCu" />
	           		<td style="padding-top:10px;">
	           			<label for="tenhinh">Tên hình:</label>&nbsp<span class="requirefield">*</span> 
	           		</td>
	           		<td style="padding-top:10px;">
	            		<input placeholder ="Nhập tên hình" tabindex="1" type="text" name="TenHinh2" class="txt" size="54" value="<?php echo $baivietcansua[0]['TenHinh']; ?>"/>
	            	</td>
	            </tr>
	            <tr>
	            	<td style="padding-top:10px;">
            			<label for="noidung">Nội dung:</label>&nbsp<span class="requirefield">*</span> 
            		</td>
            		<td style="padding-top:10px;">
            			<textarea id="NoiDung2" name="NoiDung2" rows="20" cols="36"><?php echo $baivietcansua[0]['NoiDung']; ?></textarea>		
							<script type="text/javascript">
								var editor = CKEDITOR.replace( 'NoiDung2',
								{
									// Load the German interface.
									<?php $address = "/WebQTD/scripts/ckfinder/";?>
									language: 'vi',
									// Xem
									filebrowserBrowseUrl : 		'<?php echo $address; ?>ckfinder.html',
									filebrowserImageBrowseUrl : '<?php echo $address; ?>ckfinder.html?type=Images',
									filebrowserFlashBrowseUrl : '<?php echo $address; ?>ckfinder.html?type=Flash',
									// Upload
									filebrowserUploadUrl : 		'<?php echo $address; ?>core/connector/php/connector.php?command=QuickUpload&type=Files',
									filebrowserImageUploadUrl : '<?php echo $address; ?>core/connector/php/connector.php?command=QuickUpload&type=Images',
									filebrowserFlashUploadUrl : '<?php echo $address; ?>core/connector/php/connector.php?command=QuickUpload&type=Flash',
									
									height: 200
								});
								
								CKFinder.setupCKEditor( editor, '<?php echo $address; ?>' ) ;
							</script>
					</td>
				</tr>
				<p class="requirefields">(*): Các trường bắt buộc phải có.</p>      				
            </table>
            <table style="margin-left: 320px;">
            	<tr>
					<td style="padding-top:15px" colspan="2" align="center">
				        <input class="button small blue" type="submit" name="submit" id="submit" value="Cập nhật" />
				       	&nbsp &nbsp &nbsp
				       	<input class="button small blue" type="reset" name="reset" id="reset" value="Nhập lại" />
				    </td>
            	</tr>
            </table>
         </form>
         </div>  
<?php
	if(isset($_SESSION["LoiThemBaiViet"]))
	{
		echo $_SESSION["LoiThemBaiViet"];
		unset($_SESSION["LoiThemBaiViet"]);
	}
?>
<script type="text/javascript">
    function checkPostArtForm(frm)
    {
    	if(frm.TieuDe.value == "")
    	{
    		alert('Hãy nhập tiêu đề bài viết vào khung!');
    		frm.TieuDe.focus();
    		return false;
    	}
    	if(frm.MaChuyenMuc.value == 0)
    	{
    		alert('Hãy chọn chuyên mục vào khung!');
    		return false;
    	}
    	if(frm.MaLoaiChuyenMuc.value == 0)
    	{
    		alert('Hãy chọn chủ đề vào khung!');
    		return false;
    	}
        var editorText = CKEDITOR.instances.TomTat.getData();
    	if(editorText=="")
    	{
    		alert('Hãy nhập tóm tắt bài viết vào khung! ');
    		frm.TomTat.focus();
    		return false;
    	}
    	if(frm.TenHinh.value == "")
    	{
    		alert('Hãy nhập tên hình vào khung!');
    		frm.TenHinh.focus();
    		return false;
    	}
        var editorText = CKEDITOR.instances.NoiDung.getData();
    	if(editorText == "")
    	{
    		alert('Hãy nhập nội dung bài viết vào khung!');
    		frm.NoiDung.focus();
    		return false;
    	}
    	return true;
    }
</script>
